/* 
 * File:   tcpSocket.h
 * Author: f10-kafedra
 *
 * Created on 10 Сентябрь 2011 г., 15:34
 */

#ifndef TCPSOCKET_H
#define	TCPSOCKET_H

#include <ace/INET_Addr.h>
#include <ace/SOCK_Connector.h>
#include <ace/SOCK_Stream.h>

#include <QtNetwork/QTcpSocket>
#include <QStringList>
#include <iostream>
#include <QFile>
#include <QTextStream>
#include <QByteArray>

#include "../log_lib/loglib.h"
#include "structs.h"

using namespace std;

#define MAGIC_USER_ID 31337
//#define HOST "192.168.9.120"
#define HOST "192.168.9.18"
//#define HOST "127.0.0.1"
#define PORT 6666

const size_t MAX_RECV_BUFFER_SIZE = 1500;

class tcpSocket : public QTcpSocket{
    Q_OBJECT
public:
    tcpSocket(QString login, QString pass);
    virtual ~tcpSocket();
    
    apmType apm;
    char* host;
    QString login;
    QString pass;
    QString filialID;
    QString lastBuffer;
    QString maxID;
    QString maxMFN;
    loglib* log;
    bool singleRequest;
    bool connected;
    
    requests structRequest;
    operations flagOper;
    confs configFiles;
    queueRequests request;
    vector <queueRequests> query;
    lastOperation lastOperResult;
    vector <info> inform;
    //vector <QString> buffer;
    
    bool inWork;
    
    // чит для проверки выполненных действий
    bool sended;
    bool readed;
    
    int reqCounter;
    
    int _connectToHostBase(const char* host, const int port);
    void disconnectFromBase();
    bool __sendData(string data);
    string __reciveAnswer();
    
    int connectToHostBase(QString host, int port);
    void loginToBase();
    void unloginFromBase();
    void getMaxMFN();
    void getLastID();
    void getServerVersion();
    void getUser_MFN(QString MFN, QString maxMFN);
    void getUser_FIO(QString FIO, QString maxMFN);
    void updateUserInfo(QString MFN, QString maxMFN, QString fieldsToUpdate);
    void createUser(QString maxMFN, QString userData);
    void sendData(QString data);
    void getConfFile(QString file);
    void getOVD();
    void checkIfUserExists(QStringList FIO);
    void getTarget(QString target, targetField field);
    void getTargetAmount(QString target);
    void getInfos(int from, int to);
    void sendInfos(QStringList info);
    void getUserByID(QString id);
    void updateRecord(fields field, QString parametr, QString visit);
    void getUserRecord(QString mfn);
    void unlockRecord(QString mfn);
    
private:
    ACE_SOCK_Connector connector;
    ACE_SOCK_Stream peer;
    ACE_INET_Addr peer_addr;
    char buf[BUFSIZ];
    
    
    
    
    QString loginRequest(QString codeCommand, QString codeAPM);
    QString unLoginRequest(QString codeCommand, QString codeAPM);
    QString getMaxMFN_request(QString bd);
    QString getSrvVersion();
    QString generalRequest(QString codeCommand, QString codeAPM, QString parameters);
    QString generalRequestEditEnabled(QString codeCommand, QString codeAPM, QString parameters);
    QString generalRequestEditEnabled_0a(QString codeCommand, QString codeAPM, QString parameters);
    
private slots:
    void slotConnected();
    void slotDisConnected();
    void slotReadBytes();
    
};

#endif	/* TCPSOCKET_H */

